//
//  DKFeedBackController.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/11.
//

#import "DKFeedBackController.h"

@interface DKFeedBackController ()<UITextViewDelegate>
@property (nonatomic, strong)UITextView * feedBackTextView;
@property (nonatomic, strong)UILabel * feedBackNumLab;
@property (nonatomic, strong)UIButton * feedBackSubimBtn;
@end

@implementation DKFeedBackController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    [self makeNavBack];
    [self dk_addSubView];
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_Suggestion withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
}
- (void)dk_addSubView{
    UILabel * dk_FeedBackHeadLab = [[UILabel alloc] init];
    dk_FeedBackHeadLab.textColor = DK_Hex(@"#333333");
    dk_FeedBackHeadLab.font = DK_FONT_Medium(17);
    dk_FeedBackHeadLab.text = @"Feedback";
    [self.view addSubview:dk_FeedBackHeadLab];
    [dk_FeedBackHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(27)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    UILabel * dk_FeedBackDescribeLab = [[UILabel alloc] init];
    dk_FeedBackDescribeLab.textColor = DK_Hex(@"#999999");
    dk_FeedBackDescribeLab.font = DK_FONT_Regular(14);
    dk_FeedBackDescribeLab.text = @"Please describe your feedback to us";
    [self.view addSubview:dk_FeedBackDescribeLab];
    [dk_FeedBackDescribeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_FeedBackHeadLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.left.equalTo(@(DK_IsWidthScale(27)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    UIView *dk_FeedBackDescribeView = [[UIView alloc] init];
    dk_FeedBackDescribeView.backgroundColor = DK_Hex(@"#FAFAFA");
    dk_FeedBackDescribeView.layer.cornerRadius = DK_IsWidthScale(10);
    [self.view addSubview:dk_FeedBackDescribeView];
    [dk_FeedBackDescribeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.top.equalTo(dk_FeedBackDescribeLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(200)));
    }];
    //占位符
    self.feedBackTextView = [[UITextView alloc] init];
    self.feedBackTextView.textColor = DK_Hex(@"#333333");
    self.feedBackTextView.font = DK_FONT_Regular(14);
    self.feedBackTextView.backgroundColor = UIColor.clearColor;
    self.feedBackTextView.delegate = self;
    [dk_FeedBackDescribeView addSubview:self.feedBackTextView];
    [self.feedBackTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(20)));
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.bottom.equalTo(@(-DK_IsWidthScale(36)));
    }];
    UILabel* feedBackPlaceHolderLab = [[UILabel alloc] init];
    feedBackPlaceHolderLab.textColor = DK_Hex(@"#CCCCCC");
    feedBackPlaceHolderLab.font = DK_FONT_Regular(14);
    feedBackPlaceHolderLab.text = @"Please enter";
    [feedBackPlaceHolderLab sizeToFit];
    [self.feedBackTextView addSubview:feedBackPlaceHolderLab];
    [self.feedBackTextView setValue:feedBackPlaceHolderLab forKey:@"_placeholderLabel"];
    
    self.feedBackNumLab = [[UILabel alloc] init];
    self.feedBackNumLab.font = DK_FONT_Regular(14);
    self.feedBackNumLab.textColor = DK_Hex(@"#CCCCCC");
    self.feedBackNumLab.text = @"0/160";
    [dk_FeedBackDescribeView addSubview:self.feedBackNumLab];
    [self.feedBackNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(20)));
        make.bottom.equalTo(@(-DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(16)));
    }];
    //协议
    UIButton * feedBackPrivacyAgreementBtn = [[UIButton alloc] init];
    [feedBackPrivacyAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [feedBackPrivacyAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    feedBackPrivacyAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [feedBackPrivacyAgreementBtn addTarget:self action:@selector(checkPrivacyAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:feedBackPrivacyAgreementBtn];
    [feedBackPrivacyAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(@(-DK_IsWidthScale(80)-DK_SafeAreaBottom));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    //next
    self.feedBackSubimBtn = [[UIButton alloc] init];
    [self.feedBackSubimBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [self.feedBackSubimBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.feedBackSubimBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.feedBackSubimBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.feedBackSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.feedBackSubimBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.feedBackSubimBtn.enabled = NO;
    [self.feedBackSubimBtn addTarget:self action:@selector(goCheck) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.feedBackSubimBtn];
    [self.feedBackSubimBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(feedBackPrivacyAgreementBtn.mas_top).offset(-DK_IsWidthScale(97));
    }];
}
- (void)checkPrivacyAgreement{
//    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//    dk_WebViewCtrl.webUrl = DK_PrivacyAgreement;
    DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
    [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
}
- (void)makeNextStatus:(BOOL)isEnable{
    self.feedBackSubimBtn.enabled = isEnable;
}
- (void)goCheck{
    WeakSelf(self)
    NSMutableDictionary *feedBackDic = [[NSMutableDictionary alloc] init];
    [feedBackDic setValue:self.feedBackTextView.text forKey:@"content"];
    [feedBackDic setValue:@"feedback" forKey:@"title"];
    [DKPublicHttp apiUserComplaintForAPI:feedBackDic complete:^{
        [DKAlertHelper showMessage:@"Sent successfully"];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
    }];
}
#pragma mark -- UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString * textContent = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if (textContent.length > 160) {
        return NO;
    }
    if(textContent.length > 0){
        [self makeNextStatus:YES];
    }else{
        [self makeNextStatus:NO];
    }
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
    NSInteger maxFontNum = 160;//最大输入限制
    NSString *toBeString = textView.text;
    // 获取键盘输入模式
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // zh-Hans代表简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textView markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > maxFontNum) {
                textView.text = [toBeString substringToIndex:maxFontNum];//超出限制则截取最大限制的文本
                self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/160",maxFontNum];
            } else {
                self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/160",toBeString.length];
            }
        }
    } else {// 中文输入法以外的直接统计
        if (toBeString.length > maxFontNum) {
            textView.text = [toBeString substringToIndex:maxFontNum];
            self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/160",maxFontNum];
        } else {
            self.feedBackNumLab.text = [NSString stringWithFormat:@"%ld/160",toBeString.length];
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
